package com.wei.czz.framework.admin.controller;

import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.dto.admin.userConfig.UserConfigTreeDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.admin.userConfig.UserConfigVo;
import com.wei.czz.framework.admin.manager.UserConfigManager;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-05-31 09:53:42
 * className: UserConfigController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/sys/user-config")
@AllArgsConstructor
public class UserConfigController {

    private static final Logger log = LoggerFactory.getLogger(UserConfigController.class);

    private final UserConfigManager userConfigManager;

    /**
     * 保存用户配置接口
     * <p>
     * 此接口会校验是否重复提交
     * @param userConfigVo 参数对象
     * @return 结果
     */
    @Resubmit(message = "保存用户配置")
    @PostMapping("/save")
    public Result<Object> saveUserConfig(@Validated @RequestBody UserConfigVo userConfigVo) {
        log.info("开始保存用户配置业务。{}", userConfigVo);

        userConfigManager.saveUserConfig(userConfigVo);

        log.info("保存用户配置业务结束");
        return Result.success();
    }

    /**
     * 获取用户配置接口
     * @return 结果
     */
    @Resubmit(message = "获取用户配置")
    @GetMapping("/get")
    public Result<String> getUserConfig(@RequestParam("configKey") String configKey) {
        log.info("开始获取用户配置业务。configKey={}", configKey);

        String configValue = userConfigManager.getUserConfig(configKey);

        log.info("获取用户配置业务结束");
        return Result.success(configValue);
    }

    /**
     * 获取用户配置树接口
     * @return 结果
     */
    @GetMapping("/tree")
    public Result<List<UserConfigTreeDto>> getUserConfigTree() {
        log.info("开始获取用户配置树业务");

        List<UserConfigTreeDto> tree = userConfigManager.getUserConfigTree();

        log.info("获取用户配置树业务结束");
        return Result.success(tree);
    }
}
